package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQuery;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("list")
    @RequiredPermission(name = "员工列表",expression ="employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("departments",departments);
        return "/employee/list";
    }

    @RequestMapping("input")
    @RequiredPermission(name = "员工编辑",expression ="employee:input")
    public String input(Long id,Model model){
        if (id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        List<Department> departments = departmentService.listAll();
        List<Role> roles = roleService.listAll();
        model.addAttribute("departments",departments);
        model.addAttribute("roles",roles);
        return "/employee/input";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission(name = "员工保存或更新",expression ="employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){

        try {
            if (employee.getId() != null){
                employeeService.update(employee,ids);
            }
            else {
                employeeService.insert(employee,ids);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"员工保存失败,请联系管理员");
        }
    }

    @RequestMapping("delete")
    @RequiredPermission(name = "员工删除",expression ="employee:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            if (id != null){
                employeeService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"员工删除失败,请联系管理员");
        }
    }

    @RequestMapping("updatePwd")
    public String updatePwd(){
        //获取当前用户信息
        Employee employee = UserContext.getCurrentUser();
        if (employee != null){
        return "/employee/updatePwd";
        }
        return null;
    }

    @RequestMapping("updatePwdById")
    @ResponseBody
    public JsonResult updatePwdByIdAndOldPwd(Long id,String oldPassword,String newPassword){
        try {
            if (id != null){
                employeeService.updatePwdByIdAndOldPwd(id,oldPassword,newPassword);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }

    @RequestMapping("resetPwd")
    public String resetPwd(Model model,Long id){

        if (id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/resetPwd";
    }

    @RequestMapping("resetPwdByAdmin")
    @ResponseBody
    public JsonResult resetPwdByAdmin(String newPassword,Long id){

        try {
            if (id != null ){
                employeeService.resetPwdByAdmin(newPassword,id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }
}
